
package stack.be.touch.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import stack.be.touch.db.DatabaseHelper;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.text.TextUtils;

/**
 * @author D.Kita
 */
public final class FileUtils {

    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    private FileUtils() {

    }

    public static String getFileNameWithoutExtension(String path) {
        if (!TextUtils.isEmpty(path)) {
            int start = TextUtils.lastIndexOf(path, '/') + 1;
            int end = TextUtils.lastIndexOf(path, '.');
            if (end == -1) {
                end = path.length();
            }
            return path.substring(start, end);
        }
        return path;
    }

    public static void copyDatabaseToSdCard(Context context){
    	String databaseDirPath = "/data/data/" + context.getPackageName() + "/databases";
    	File databaseFile = new File(databaseDirPath,DatabaseHelper.DATABASE_NAME);
    	if(databaseFile.exists()){
    		try {
				FileInputStream inputStream = new FileInputStream(databaseFile);
				File SDCardRoot = Environment.getExternalStorageDirectory();
				File outputDB = new File(SDCardRoot,"test.db");
				FileOutputStream outputStream = new FileOutputStream(outputDB);
				
				byte[] buf = new byte[1024];
	            int n;
	            while ((n = inputStream.read(buf, 0, 1024)) > -1) {
	            	outputStream.write(buf, 0, n);
	            }
	            
	            outputStream.flush();
	            
	            outputStream.close();
	            inputStream.close();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    }
    
    
    public static String getFileName(String path) {
        if (!TextUtils.isEmpty(path)) {
            int start = TextUtils.lastIndexOf(path, '/') + 1;
            int end = path.length();
            return path.substring(start, end);
        }
        return path;
    }

    public static String getZipFileName(Context context) throws IOException{
        AssetManager assetManager = context.getAssets();
        String zipFileName = null;
        for (String fileName : assetManager.list("")) {  
        	if (fileName.endsWith(".zip")) {
                System.out.println("FileName : " + fileName);
                zipFileName = fileName;
            }
        }
        return zipFileName;
    }
    public static void extractZipFile(Context context, String destPath, String zipName)
            throws IOException {

        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(zipName, AssetManager.ACCESS_STREAMING);
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipEntry zipEntry = zipInputStream.getNextEntry();

        while (zipEntry != null) {
            String entryName = zipEntry.getName();
            FileOutputStream fileOutputStream = new FileOutputStream(destPath + "/" + entryName);
            byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
            int n;
            while ((n = zipInputStream.read(buf, 0, DEFAULT_BUFFER_SIZE)) > -1) {
                fileOutputStream.write(buf, 0, n);
            }

            fileOutputStream.close();
            zipInputStream.closeEntry();
            zipEntry = zipInputStream.getNextEntry();
        }
    }
    
    public static ArrayList<String> extractDBZipFile(Context context, String filePath, String unzipDir) throws IOException {
    	ZipInputStream zipInputStream = null;
		FileOutputStream fileOutputStream = null;
		ArrayList<String> entrysUnzip = new ArrayList<String>();
		try {
	    	zipInputStream = null;
	    	fileOutputStream = null;
	    	
	    	File file = new File(filePath);
	    	InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
	    	
	    	zipInputStream = new ZipInputStream(inputStream);
	        ZipEntry zipEntry = zipInputStream.getNextEntry();
	        
	        while (zipEntry != null) {
	        	String entryName = zipEntry.getName();
	        	File unzipEntryPath = new File(unzipDir, entryName);
	        	fileOutputStream = new FileOutputStream(unzipEntryPath);
	        	byte[] buf = new byte[1024];
	            int n;
	            while ((n = zipInputStream.read(buf, 0, 1024)) > -1) {
	                fileOutputStream.write(buf, 0, n);
	            }
	            fileOutputStream.flush();
	            fileOutputStream.close();
	            fileOutputStream = null;
	            zipEntry = zipInputStream.getNextEntry();
	            entrysUnzip.add(unzipDir+"/"+entryName);
	        }
			zipInputStream.close();
			fileOutputStream = null;
			zipInputStream = null;
		}
		finally {
			
			if(fileOutputStream!=null) try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			if(zipInputStream!=null) try {
				zipInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
    	
        return entrysUnzip;
        
    }

    public static void copyFileFromSd(String fileName, String destPath) throws IOException {
        File externalStorage = Environment.getExternalStorageDirectory();
        File srcFile = new File(externalStorage.getPath() + "/" + fileName);

        InputStream inputStream = new FileInputStream(srcFile);
        FileOutputStream fileOutputStream = new FileOutputStream(destPath);
        byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
        int n;
        while ((n = inputStream.read(buf, 0, DEFAULT_BUFFER_SIZE)) > -1) {
            fileOutputStream.write(buf, 0, n);
        }
        inputStream.close();
        fileOutputStream.close();
    }
}
